package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.GetMachineBo;
import org.dromara.system.domain.bo.SysUserGetMachineRecordsBo;
import org.dromara.system.domain.vo.SysUserGetMachineRecordsVo;
import org.dromara.system.service.ISysUserGetMachineRecordsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 领取机具记录
 *
 * @author Lion Li
 * @date 2025-07-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/userGetMachineRecords")
public class SysUserGetMachineRecordsController extends BaseController {

    private final ISysUserGetMachineRecordsService sysUserGetMachineRecordsService;

    /**
     * 查询领取机具记录列表
     */
//    @SaCheckPermission("system:userGetMachineRecords:list")
    @GetMapping("/list")
    public TableDataInfo<SysUserGetMachineRecordsVo> list(SysUserGetMachineRecordsBo bo, PageQuery pageQuery) {
        return sysUserGetMachineRecordsService.queryPageList(bo, pageQuery);
    }

    //前台
    @GetMapping("/listReception")
    public TableDataInfo<SysUserGetMachineRecordsVo> listReception(SysUserGetMachineRecordsBo bo, PageQuery pageQuery) {
        return sysUserGetMachineRecordsService.listReception(bo, pageQuery);
    }

    /**
     * 导出领取机具记录列表
     */
//    @SaCheckPermission("system:userGetMachineRecords:export")
    @Log(title = "领取机具记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysUserGetMachineRecordsBo bo, HttpServletResponse response) {
        List<SysUserGetMachineRecordsVo> list = sysUserGetMachineRecordsService.queryList(bo);
        ExcelUtil.exportExcel(list, "领取机具记录", SysUserGetMachineRecordsVo.class, response);
    }

    /**
     * 获取领取机具记录详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("system:userGetMachineRecords:query")
    @GetMapping("/{id}")
    public R<SysUserGetMachineRecordsVo> getInfo(@NotNull(message = "主键不能为空")
                                                 @PathVariable Long id) {
        return R.ok(sysUserGetMachineRecordsService.queryById(id));
    }

    /**
     * 新增领取机具记录
     */
//    @SaCheckPermission("system:userGetMachineRecords:add")
    @Log(title = "领取机具记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysUserGetMachineRecordsBo bo) {
        return toAjax(sysUserGetMachineRecordsService.insertByBo(bo));
    }

    /**
     * 修改领取机具记录
     */
//    @SaCheckPermission("system:userGetMachineRecords:edit")
    @Log(title = "领取机具记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysUserGetMachineRecordsBo bo) {
        return toAjax(sysUserGetMachineRecordsService.updateByBo(bo));
    }

    /**
     * 删除领取机具记录
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("system:userGetMachineRecords:remove")
    @Log(title = "领取机具记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysUserGetMachineRecordsService.deleteWithValidByIds(List.of(ids), true));
    }

    //查询获取机具价格
    @PostMapping("/queryGetMachinePrice")
    public R<BigDecimal> queryGetMachinePrice() {
        return R.ok("查询成功！", sysUserGetMachineRecordsService.queryGetMachinePrice());
    }

    //拉起领取机具
    @PostMapping("/getMachine")
    public R<String> getMachine(@RequestBody GetMachineBo getMachineBo) {
        return R.ok("拉起支付成功成功！", sysUserGetMachineRecordsService.getMachine(getMachineBo));
    }

    @PostMapping(value = "/getMachine/callBak/wxPay")
    @SaIgnore
    public String wxPayCallback(HttpServletRequest request, HttpServletResponse response) {
        return sysUserGetMachineRecordsService.wxPayCallback(request, response);
    }


}
